﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Web.Security;
using System.IO;

namespace Comm
{
    /// <summary>
    /// DES加密类
    /// </summary>
    public class DES
    {
        private static string _sKey = "md5";  //默认key
        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="pToEncrypt">需加密字符串</param>
        /// <param name="sKey">密钥</param>
        /// <returns>加密后字符串</returns>
        public static string EncryptDES(string pToEncrypt, string sKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
            des.Key = ASCIIEncoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            des.IV = ASCIIEncoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();
            foreach (byte b in ms.ToArray())
            {
                ret.AppendFormat("{0:X2}", b);
            }
            return ret.ToString();
        }

        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="pToEncrypt">需加密字符串</param>
        /// <returns>加密后字符串</returns>
        public static string EncryptDES(string pToEncrypt)
        {
            return EncryptDES(pToEncrypt, _sKey);
        }

        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="pToDecrypt">需解密字符串</param>
        /// <param name="sKey">密钥</param>
        /// <returns>解密后字符串</returns>
        public static string DecryptDES(string pToDecrypt, string sKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
            for (int i = 0; i < pToDecrypt.Length / 2; i++)
            {
                int j = (Convert.ToInt32(pToDecrypt.Substring(i * 2, 2), 16));
                inputByteArray[i] = (byte)j;
            }
            des.Key = ASCIIEncoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            des.IV = ASCIIEncoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();
            return Encoding.Default.GetString(ms.ToArray());
        }

        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="pToDecrypt">需解密字符串</param>
        /// <returns>解密后字符串</returns>
        public static string DecryptDES(string pToDecrypt)
        {
            return DecryptDES(pToDecrypt, _sKey);
        }

    }
}
